# Time-stamp: <09 May 00 10:30:02 giron1

=head1 NAME

SIDL_SUBLAT - Package for creation of sub lattices -

=head1 SYNOPSIS

 use SIDL_SUBLAT;

 SIDL_SUBLAT::sublat(\$node,\@array_g,\%node_hash,\@link_array);

=head1 DESCRIPTION

 It creates a sublattice from a array in input.

 This program is recursively called if inside the array there are
 sub-arrays (sublattice ).

 It uses the followings global variables: (defined in the main)

 I<$node>
 I<@array_g>
 I<%node_hash>
 I<@link_array>

=head1 AUTHOR

 $Id: SIDL_SUBLAT.pm,v 1.1 2007/10/08 09:26:03 schweitz Exp $
 Daniela Raddino, Stefan Rapp

=head1 LOG

 $Log: SIDL_SUBLAT.pm,v $
 Revision 1.1  2007/10/08 09:26:03  schweitz
 version 2.4, lib vergessen

 Revision 1.1  2000/05/12 15:01:26  giron1
 First package version


=cut

package SIDL_SUBLAT;

#----------------------------------------------------------------------

=head1 FUNCTIONS

Here is a description of the functions used

=head2 sublat


=cut

sub sublat {
  my ($node_ref,$sub_array_ref,$node_hash_ref,$link_array_ref)=@_;

  my $length=scalar(@$sub_array_ref);

  #Creation of loop----------------------------------------

  my $pointer_count=$sub_array_ref->[0];
  my $mincount=$pointer_count->{mincount};
  my $maxcount=$pointer_count->{maxcount};
  my ($start,$end)=create_loop($mincount,$maxcount,$node_ref,
			       $node_hash_ref,$link_array_ref);
  
  #Start to read the array----------------------------------
  
  my $i;
  my $last = $start;
  for ($i=1;$i<=$length-1;$i++) {   
    if ($sub_array_ref->[$i] eq "|") {
       create_link($link_array_ref,$last,$end); 
       $last=$start;       
    } else {
      if (not defined($sub_array_ref->[$i][0])) {
	# create a new node and the link
	++$$node_ref; 
       	create_node($node_hash_ref,$$node_ref,$sub_array_ref->[$i]);
    	create_link($link_array_ref,$last,$$node_ref);
	$last=$$node_ref;
      } else {
        create_link($link_array_ref,$last,$$node_ref+1);
        $last=$$node_ref + 2;
        $pointer=$sub_array_ref->[$i];
        sublat($node_ref,$pointer,$node_hash_ref,$link_array_ref);
      }#end of else 
    }#end of else 
  }#end of for
  create_link($link_array_ref,$last,$end);

  
}#end of 

=head2 create_node


=cut

#subroutines from here-----------------------------------------------
sub create_node   {
  my ($node_hash_ref,$node_counter,$node_word)=@_ ;      
  $node_hash_ref->{$node_counter}=$node_word;
}

=head2 create_link


=cut
 
sub create_link {
  my ($link_array_ref) = shift;
  my ($first,$second)=@_;
  push @$link_array_ref,[$first,$second];
} 

=head2 create_loop


=cut

sub create_loop {
  my ($min,$max,$node_ref,$node_hash_ref,$link_array_ref)=@_;
  my ($start,$end);
  my (@retvalue);
  if ((not defined($max)) && ($min==0)) {
    #create the special node
    my $special=++$$node_ref;
    create_node($node_hash_ref,$$node_ref,"!NULL");

    #create the end node
    $end=++$$node_ref;
    create_node($node_hash_ref,$$node_ref,"!NULL");

    #create the start node
    $start=++$$node_ref;
    create_node($node_hash_ref,$$node_ref,"!NULL");  
 
    create_link($link_array_ref,$special,$start);
    create_link($link_array_ref,$special,$end);
    create_link($link_array_ref,$end,$start);
   
    @retvalue=($start,$end);
   
  } else {

    #create the start node
    $start=++$$node_ref;
    create_node($node_hash_ref,$$node_ref,"!NULL");
  
    #create the end node
    $end=++$$node_ref;
    create_node($node_hash_ref,$$node_ref,"!NULL");
     
    #create the loop
    if (($min==0) && ($max==1)) {
      create_link($link_array_ref,$start,$end);}
 
    @retvalue=($start,$end);
  }
  return @retvalue;
}#end of sub routine make_loop

1;



